/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: LoginServiceImpl
 * @Package com.rx.zuul.service
 * @Description: 登录服务
 * @author: 陈锦韬
 * @date: 2021\6\28 0028
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.zuul.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rx.core.base.RxBaseConstant;
import com.rx.core.base.RxBaseController;
import com.rx.core.base.RxBaseException;
import com.rx.core.base.RxResponse;
import com.rx.core.bean.LoginUserInfo;
import com.rx.core.common.SecretInfo;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.enm.RX_LOG_MODE;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import com.rx.zuul.bean.*;
import com.rx.zuul.service.FeignLoginService;
import com.rx.zuul.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author: Administrator
 * @Description: 登录服务
 * @date: 2021\6\28 0028
 */
@Service
@Slf4j
public class LoginServiceImpl extends RxBaseController implements LoginService {

    @Autowired
    FeignLoginService feignLoginService;

    @Autowired
    ZuulConfig zuulConfig;

    @Autowired
    RxTokenConfig rxTokenConfig;

    public static volatile String CLIENT_SECRET;

    private String getClientSecret(){
        // TODO:存放于redis
       if (EmptyChecker.isEmpty(CLIENT_SECRET)){
           SecretInfo recretInfo = feignLoginService.secret();
           CLIENT_SECRET = recretInfo.getSecret();
       }
       return CLIENT_SECRET;
    }

    @Override
    public LoginInfo login(LoginInfoVo loginInfoVo) {
        RxUtil.nullDo(loginInfoVo.getLoginClass(),()->loginInfoVo.setLoginClass("username"));
        LoginInfoFromUua loginInfoFromUua = feignLoginService.getToken(
                loginInfoVo.getLoginClass(),
                getClientSecret(),
                "password",
                loginInfoVo.getCaptcha(),
                null,
                loginInfoVo.getUserName() + (EmptyChecker.isEmpty(loginInfoVo.getOrgBid())?"": RxBaseConstant.SPLIT_STRING + loginInfoVo.getOrgBid()),
                loginInfoVo.getPassword());
        RxUtil.CheckUtil.checkResult(loginInfoFromUua,RESULT_CODE.FAIL);

        OAuth2AccessToken oAuth2AccessToken = rxTokenConfig.tokenStore().readAccessToken(loginInfoFromUua.getValue());
        // 获取当前用户信息
        DefaultOAuth2AccessToken oAuth2Authentication = (DefaultOAuth2AccessToken)oAuth2AccessToken;
        String userInfoJson = (String)oAuth2Authentication.getAdditionalInformation().get("user_name");
        LoginUserInfo principal = JSON.parseObject(userInfoJson,LoginUserInfo.class);
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setAccessToken(loginInfoFromUua.getValue());
        loginInfo.setRefreshToken(loginInfoFromUua.getRefreshToken().getValue());
        loginInfo.setJti((String)loginInfoFromUua.getAdditionalInformation().get("jti"));
        loginInfo.setUserInfo(principal);
        return loginInfo;
    }
}
